package br.ufc.pet.controller;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import br.ufc.pet.dao.UsuarioDao;
import br.ufc.pet.dao.UsuarioJPADao;
import br.ufc.pet.model.Usuario;
import br.ufc.pet.util.ReportUtil;

@ManagedBean
public class EntregaCertificadoBean {

	private Usuario usuario;
	private String msg;

	public EntregaCertificadoBean() {
		this.usuario = new Usuario();
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void gerarCertificado() throws IOException {
		try {
			HttpServletRequest request = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();

			String user = request.getParameter("user");

			UsuarioDao uDao = new UsuarioJPADao();
			this.usuario = uDao.buscarPorCodigo(user);
			this.usuario.setVisualizado(true);
			uDao.update(this.usuario);
			
			FacesContext facesContext = FacesContext.getCurrentInstance();
			facesContext.responseComplete();
			ServletContext scontext = (ServletContext) facesContext
					.getExternalContext().getContext();
			String path = scontext.getRealPath("/WEB-INF/relatorios/certificado.jrxml");
			
			byte[] bytes = ReportUtil.geraCertificado(usuario, path);
			if(bytes != null && bytes.length > 0){
				HttpServletResponse response = (HttpServletResponse) facesContext
						.getExternalContext().getResponse();

				response.setContentType("application/pdf");

				response.setHeader("Content-disposition",
						"inline; filename=\"relat.pdf\"");

				response.setContentLength(bytes.length);

				ServletOutputStream outputStream;
				outputStream = response.getOutputStream();

				outputStream.write(bytes);
				
				outputStream.flush();

				outputStream.close();

			}
				
			
		} catch (JRException jr){
			jr.printStackTrace();
			System.out.println("Erro 1");
			FacesContext facesContext = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) facesContext
					.getExternalContext().getResponse();
			this.msg = jr.getMessage();
			HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest();
			String path = request.getContextPath();
			response.sendRedirect("erro.xhtml?msg="+ this.msg);
			
		}
		catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Erro2");
			FacesContext facesContext = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) facesContext
					.getExternalContext().getResponse();
			this.msg = ex.getMessage();
			HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest();
			String path = request.getContextPath();
			response.sendRedirect("erro.xhtml?msg="+ this.msg);
		}
	}

}
